#include "Tool.h"

int GetRandNumber(int max)
{
    return rand() % max;
}

void InitBoard(char (*p)[Map_Size])
{
    for (int i = 0; i < Map_Size; i++)
    {
        for (int j = 0; j < Map_Size; j++)
        {
            p[i][j] = '-';
        }
    }
}

void PrintMap(char (*p)[Map_Size], struct Player *player)
{
    InitBoard(p);

    p[player->y][player->x] = 'X';

    printf("    ");
    for (int i = 0; i < Map_Size; i++)
    {
        printf(" %5d", i + 1);
    }
    printf("\n");
    for (int i = 0; i < Map_Size; i++)
    {
        printf("%4d", i + 1);
        for (int j = 0; j < Map_Size; j++)
        {
            printf("  %4c", p[j][i]);
        }
        printf(" \n");
    }
}

struct LuckyWheel *GetRandomLWheel(struct DynamicArray *array)
{
    // 从数组中随机选择一个宝物
    int index = rand() % array->len;
    printf("抽到了%d号奖品\n",index);
    return (struct LuckyWheel*)array->dp[index];
}


//注册哎
void FreeAccount(DLlist *list)
{
    while(list->head != NULL)
    {
        struct Node *Free = list->head;
        list->head = list->head->next;
        char *temp = (char*)Free->data;
        free(temp);
        free(Free);  
    }
}

int  SavePlayerAccount(char *account,char *passwd)//保存玩家账号，密码
{
    FILE *file = fopen("PlayerAccount.txt","a");//a追加写
    if(file == NULL)
    {
        printf("file open error\n");
        return false;
    }

    DLlist *list = GetLineFromFile("PlayerAccount.txt");
    struct Node *TravelPoint = list->head;
    while(TravelPoint != NULL)
    {   
        char *temp = (char*)TravelPoint->data;
        char *ch1 = strtok(temp,":");//存储字符account
        char *SavedAccount = strtok(NULL," ");//用于匹配的账号
        char *ch2 = strtok(NULL,":");//存储字符passwd
        char *SavedPasswd = strtok(NULL,":");//用于匹配的密码
        if(strcmp(SavedAccount,account) == 0)
        {
            printf("账号已存在,请重新输入\n");
            FreeAccount(list);
            return false;
        }
        TravelPoint = TravelPoint->next;
    }
    fprintf(file,"account:%s passwd:%s\n",account,passwd);//格式化输入
    fclose(file);
    FreeAccount(list);
    return true;
}

int Login(char *account,char *passwd)
{
    
    DLlist *list = GetLineFromFile("PlayerAccount.txt");
    struct Node *TravelPoint = list->head;
    while(TravelPoint != NULL)
    {   
        char *temp = (char*)TravelPoint->data;
        char *ch1 = strtok(temp,":");//存储字符account
        char *SavedAccount = strtok(NULL," ");//用于匹配的账号
        char *ch2 = strtok(NULL,":");//存储字符passwd
        char *SavedPasswd = strtok(NULL,":");//用于匹配的密码
        if((strcmp(SavedAccount,account) == 0) && (strcmp(SavedPasswd,passwd) == 0))
        {
            system("clear");//在头文件stdlib.h中
            printf("=========================================================================\n");
            printf("login success\n");
            printf("Master %s welcome back\n",SavedAccount);
            printf("=========================================================================\n");
            sleep(2);
            FreeAccount(list);
            return true;
        }
        TravelPoint = TravelPoint->next;
    }
    printf("=========================================================================\n");
    printf("wrong account or passwd,please try again!\n");
    printf("=========================================================================\n");
    
    #include <string.h>
    FreeAccount(list);
    return false;
}



void Start()
{
    int choice;
    printf("1.登录\n");
    printf("2.注册\n");
    printf("3.删除账号\n");
    printf("4.退出魔塔\n");
    scanf("%d",&choice);
    while(getchar()!='\n');

    switch(choice)
    {
        case 1:
        
            int a = 0;
            while(a != true)
            {
                char account1[100]={0};
                char passwd1[100] = {0};
                printf("请输入账号:\n");
                scanf("%s",account1);
                printf("请输入密码\n");
                scanf("%s",passwd1);
                a = Login(account1,passwd1);  
                 
            }
            break;
        case 2:

            int b = 0;
            while(b != true)
            {
                char account2[100]={0};
                char passwd2[100] = {0};
                printf("请输入账号:\n");
                scanf("%s",account2);
                printf("请输入密码\n");
                scanf("%s",passwd2);
                b = SavePlayerAccount(account2,passwd2);
            }
            break;
        case 3:
                int c = 0;
                char account2[100]={0};
                char passwd2[100] = {0};
                printf("请输入要删除的账号:\n");
                scanf("%s",account2);
                printf("请输入要删除密码\n");
                scanf("%s",passwd2);
                //DeleAccount(account2,passwd2);
        case 4:
            exit(0);
        default:
            printf("输入非法,请重新输入\n");
            Start();
            break;
    }
}